#pragma once 
#include<string>
#include<unordered_map>
#include <fstream>
#include <iostream>
#include<unistd.h>
#include"Log.hpp"
#include"InetAddr.hpp"



const std::string defaultpath="dictionary.txt";
const std::string sep=": ";



namespace kzz
{
    class Dict
    {
        public:
        Dict(const std::string& dict=defaultpath):_dict_path(dict)
        {}
        void LoadDict()
        {
            //std::ifstream file(_dict_path.c_str());
            std::ifstream file("dictionary.txt");
            if( !file.is_open())
            {
                LOG(LogLevel::FATAIL)<<"字典文件打开失败！ ";
                exit(1);
            }
            std::string line;

            while(getline(file,line))
            {
                int n=line.find(sep.c_str());
                if(n==line.size())
                {
                    LOG(LogLevel::DBUGER)<<"字典文件输入错误，没有分隔符,解析失败";
                    continue;
                }
                std::string _chinese;
                std::string _english;
                _english=line.substr(0,n);
                _chinese=line.substr(n+sep.size());
                if(_chinese.empty()||_english.empty())
                {
                    LOG(LogLevel::DBUGER)<<"没有有效的信息";
                    continue;
                }
                
                _dict.insert(make_pair(_english,_chinese));
              //  std::cout<<_english<<"   "<<_chinese<<std::endl;
                LOG(LogLevel::DBUGER)<<"加载"<<line;
            }
            file.close();
        }
        // 翻译，传入要翻译的单词和客户端
       
       const  std::string TransLate(const std::string &word,InetAddr& client)
        {
            auto ite=_dict.find(word);
            if(ite==_dict.end())
            {
                LOG(LogLevel::DBUGER)<<"进入到翻译模块"<<client.Ip()<<" // "<<client.Port()<<word<<"->Node";
                return "None";
                

            }
             LOG(LogLevel::DBUGER)<<"进入到翻译模块"<<client.Ip()<<" /////// "<<client.Port()<<word<<ite->second;
                return ite->second;
        }

        ~Dict()
        {}



        private:
        std::string _dict_path;
        std::unordered_map<std::string,std::string> _dict;

    };
}